package com.example.lenovo.appstore.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.lenovo.appstore.R;
import com.example.lenovo.appstore.activity.AppInfoActivity;
import com.example.lenovo.appstore.activity.WebViewActivity;
import com.example.lenovo.appstore.utils.AppContext;
import com.example.lenovo.model.CategoryListModel;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 作者：tb on 2019/3/13.
 * 邮箱：
 * 版本：
 */

public class RankListAdapter extends RecyclerView.Adapter<RankListAdapter.ViewHolder> {
    private int ITEM = 1;//普通的item
    private int ADVERTISING = 2;//广告位的item
    private Context context;
    private List<CategoryListModel.CategoryList> data;
    private int code;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public RankListAdapter(Context context, List<CategoryListModel.CategoryList> data) {
        this.context = context;
        this.data = data;

    }

    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == ITEM) {//普通item显示的布局
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_hot, parent, false);
        } else if (viewType == ADVERTISING) {//广告位item显示的布局（自定义的广告位布局）
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_advertisement, parent, false);

        }
        return new ViewHolder(view);
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 5 || position == 8)//如果position==3/6，返回广告位图片，否则返回普通的item
            return ADVERTISING;
        else
            return ITEM;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        if (getItemViewType(position) == ITEM) {


//        holder.name.setText(data.get(position).getName());
//        Glide.with(context).load(URL.baseURL + data.get(position).getImg()).into(holder.icon);

            holder.hot.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                Intent intent = new Intent(context, SListActivity.class);
//                intent.putExtra("id", data.get(position).getC_id() + "");
//                context.startActivity(intent);
                }
            });
            holder.hot.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(AppContext.getInstance(), AppInfoActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    Log.e("点击====", position + "");
                    context.startActivity(intent);
                }
            });
            switch (getCode()) {
                case 1:
                    holder.install.setText("安装");
                    break;
                case 2:
                    holder.install.setText("¥10.00");
                    break;
                case 3:
                    holder.install.setText("安装");
                    break;
            }
        } else {
            holder.hot.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent1 = new Intent(context, WebViewActivity.class);
                    intent1.putExtra("url", "http://i.meituan.com/");
                    context.startActivity(intent1);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView name, install;
        private ImageView icon;
        private RelativeLayout hot;

        public ViewHolder(View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.title);
            icon = itemView.findViewById(R.id.icon);
            hot = itemView.findViewById(R.id.gen);
            install = itemView.findViewById(R.id.install);

        }
    }
}

